package com.ls.blog.controller;


import static com.ls.blog.common.consts.Methods.*;
import static com.ls.blog.common.consts.RedisKeyConst.CAPTCHA;

import com.google.code.kaptcha.Producer;
import com.ls.blog.common.valid.Add;
import com.ls.blog.common.valid.Select;
import com.ls.blog.dto.Recodes;
import com.ls.blog.dto.UserDTO;
import com.ls.blog.dto.VerifyCode;
import com.ls.blog.service.UserService;
import com.ls.blog.utils.R;
import com.ls.blog.utils.RedisUtil;
import com.ls.blog.utils.Result;
import com.ls.blog.vo.PageVO;
import com.ls.blog.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.codec.Base64;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lishun
 * @since 2022-01-14
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户模块")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    Producer producer;

    @Autowired
    RedisUtil redisUtil;


    @GetMapping("/getVeriCode")
    @ApiOperation(value = "验证码",httpMethod = GET)
    public Result<VerifyCode> getVerifyCode() throws IOException {
        ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
        String text = producer.createText();
        String base64 = "data:image/jpg;base64,";
        BufferedImage image = producer.createImage(text);
        ImageIO.write(image, "jpg", jpegOutputStream);
        base64 += new BASE64Encoder().encode(jpegOutputStream.toByteArray());
        jpegOutputStream.close();
        String uuid =  CAPTCHA+ UUID.randomUUID();
        redisUtil.set(uuid,text,60*5);
        VerifyCode code = VerifyCode.builder()
                .uuid(uuid)
                .img(base64)
                .build();
        return R.ok(code);

    }




    @PostMapping("/admin/list")
    @ApiOperation(value = "后台用户列表",httpMethod = POST)
    public Result<Recodes<UserDTO>> list(@RequestBody PageVO pageVO){
        return R.ok(userService.userDTOList(pageVO));
    }

    @PutMapping("/admin/editRole")
    @ApiOperation(value = "后台修改用户角色",httpMethod = PUT)
    public Result<?> editRole(@RequestBody UserVO userVO){
        userService.editRole(userVO);
        return R.ok();
    }

    @PostMapping("/home/register")
    @ApiOperation(value = "用户注册",httpMethod = POST)
    public Result<?> register(@Validated(value = Add.class) @RequestBody UserVO userVO){
        userService.register(userVO);
        return R.ok();
    }

    @PostMapping("/home/getValidCode")
    @ApiOperation(value = "注册邮箱验证码",httpMethod = POST)
    public Result<?> getValidCode(@Validated(value = Select.class) @RequestBody UserVO userVO){
        userService.getValidCode(userVO);
        return R.ok();
    }











}
